<?php


namespace App\HttpController;
use App\Common\lib\Code;
use App\Exception\ApiException;
use App\Http\Logic\TextLogic;
use App\Logic\ToolsLogic;
use EasySwoole\EasySwoole\Config as GlobalConfig;
use EasySwoole\EasySwoole\Logger;
use EasySwoole\Http\AbstractInterface\Controller;
use Illuminate\Http\Request;

class Tools extends Base
{
    /**
     * ip查询
     * @return bool
     */
    public function ip()
    {
        try {
            $ip = $this->request()->getRequestParam('ip');
            if (empty($ip)) {
                return $this->writeJson(Code::$success, null, 'ip不存在');
            }
            return $this->writeJson(Code::$success, (new ToolsLogic())->ip($ip), 'ok');
        } catch (\Exception $exception) {
            if ($exception instanceof ApiException) {
                return $this->writeJson(Code::$error, null, $exception->getMessage());
            }
            Logger::getInstance()->error($exception->getMessage());
            return $this->writeJson(Code::$error, null, "系統异常");
        }
    }

    /**
     * 在线翻译
     */
    public function fanyi()
    {
        try {
            $q = $this->request()->getRequestParam('q');
            $to = $this->request()->getRequestParam('to');
            $form = $this->request()->getRequestParam('form');
            return $this->writeJson(Code::$success, (new ToolsLogic())->fanyi($q, $to, $form), 'ok');
        } catch (\Exception $exception) {
            if ($exception instanceof ApiException) {
                return $this->writeJson(Code::$error, null, $exception->getMessage());
            }
            Logger::getInstance()->error($exception->getMessage());
            return $this->writeJson(Code::$error, null, "系統异常");
        }
    }

    /**
     * 汉字转拼音
     */
    public function pinyin()
    {
        $pinyin = $this->request()->getRequestParam('text');
        try {
            return $this->writeJson(Code::$success, (new ToolsLogic())->pinyin($pinyin), 'ok');
        } catch (\Exception $exception) {
            if ($exception instanceof ApiException) {
                return $this->writeJson(Code::$error, null, $exception->getMessage());
            }
            Logger::getInstance()->error($exception->getMessage());
            return $this->writeJson(Code::$error, null, "系統异常");
        }
    }

    /**
     * 日期间隔计算
     * @return string[]
     * @throws ApiException
     */
    public function dateCalculation()
    {
        $start_time = $this->request()->getRequestParam('start_time');
        $end_time = $this->request()->getRequestParam('end_time');
        if (empty($start_time)) throw new ApiException("开始时间不能为空");
        if (empty($end_time)) throw new ApiException("结束时间不能为空");
        if (strtotime($start_time) > strtotime($end_time)) {
            throw new ApiException("开始时间必须小于结束时间,或者日期不合法");
        }
        $year = floor((strtotime($end_time) - strtotime($start_time)) / 86400 / 365);
        $zhou = floor((strtotime($end_time) - strtotime($start_time)) / 86400 / 7);
        $date = floor((strtotime($end_time) - strtotime($start_time)) / 86400);
        $hour = floor((strtotime($end_time) - strtotime($start_time)) / 3600);
        $minute = floor((strtotime($end_time) - strtotime($start_time)) / 3600 * 60);
        $second = floor((strtotime($end_time) - strtotime($start_time)) / 3600 * 60 * 60);
        $data = [
            "start_time" => "开始时间:" . $start_time,
            "end_time" => "结束时间:" . $end_time,
            'year' => "相差年数:" . $year . '年',
            'zhou' => "相差周:" . $zhou . '周',
            'day' => '相差天数:' . $date . '天',
            'hour' => '相差小时:' . $hour . '小时',
            'minute' => '相差分钟:' . $minute . '分钟',
            'second' => '相差秒:' . $second . '秒',
        ];
        return $this->writeJson(Code::$success, $data, "OK");
    }

    /**
     * RMB大写转换
     * @return bool
     */
    public function rmb()
    {
        $text = $this->request()->getRequestParam('text');
        try {
            return $this->writeJson(Code::$success, (new ToolsLogic())->rmb($text), 'ok');
        } catch (\Exception $exception) {
            if ($exception instanceof ApiException) {
                return $this->writeJson(Code::$error, null, $exception->getMessage());
            }
            Logger::getInstance()->error($exception->getMessage());
            return $this->writeJson(Code::$error, null, Code::$system_text);
        }
    }

    /**
     * 新华字典
     * @return bool
     */
    public function dictionary(){
        $text = $this->request()->getRequestParam('text');
        try {
            return $this->writeJson(Code::$success, (new ToolsLogic())->dictionary($text), 'ok');
        } catch (\Exception $exception) {
            if ($exception instanceof ApiException) {
                return $this->writeJson(Code::$error, null, $exception->getMessage());
            }
            Logger::getInstance()->error($exception->getMessage());
            return $this->writeJson(Code::$error, null, Code::$system_text);
        }
    }

    /**
     * 高校查询
     * @return bool
     */
    public function school(){
        $all = $this->request()->getRequestParam();
        return $this->writeJson(Code::$success,(new ToolsLogic())->school($all),'ok');
    }

    /**
     * 二维码生成
     * @return bool
     */
    public function qrcode(){
        $all = $this->request()->getRequestParam();
        return $this->writeJson(Code::$success,(new ToolsLogic())->qrcode($all),'ok');
    }

    /**
     * 号码归属地查询
     * @return bool
     */
    public function phoneArea(){
        $all = $this->request()->getRequestParam();
        return $this->writeJson(Code::$success,(new ToolsLogic())->phoneArea($all),'ok');
    }
}